package com.app.couponsharelife.ui.adapter

import android.graphics.Paint
import androidx.databinding.DataBindingUtil
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.PageTipBean
import com.app.couponsharelife.data.bean.TaobaoGoodBean
import com.app.couponsharelife.databinding.GoodListItemBinding
import com.app.couponsharelife.databinding.GoodListItemNoCouponBinding
import com.app.couponsharelife.ui.listener.OnClickListener
import java.text.DecimalFormat

/**
 * Created by guluwa on 2018/11/24.
 */
class HomeTypeGoodAdapter(val list: MutableList<Any>,
                          private val listener: OnClickListener) :
        androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    fun addData(list: List<Any>, page: Int) {
        if ((this.list.isNotEmpty() && this.list[0] is PageTipBean) || page == 2) {
            this.list.clear()
        }
        this.list.addAll(list)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return when (viewType) {
            0 -> ViewHolderTip(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.page_list_tip_item, parent, false))
            1 -> ViewHolder(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.good_list_item, parent, false))
            else -> ViewHolderNoCoupon(DataBindingUtil.inflate(
                    LayoutInflater.from(parent.context), R.layout.good_list_item_no_coupon, parent, false))
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (list[position] is PageTipBean) 0 else {
            if ((list[position] as TaobaoGoodBean).has_coupon) 1 else 2
        }
    }

    override fun onBindViewHolder(holder: androidx.recyclerview.widget.RecyclerView.ViewHolder, position: Int) {
        when {
            getItemViewType(position) == 0 -> (holder as ViewHolderTip).databinding.pageTipBean = list[position] as PageTipBean
            getItemViewType(position) == 1 -> {
                val df = DecimalFormat("#0.00")
                val good = list[position] as TaobaoGoodBean
                holder as ViewHolder
                Glide.with(holder.itemView).asBitmap()
                        .apply(RequestOptions()
                                .placeholder(R.drawable.ic_good_place_holder)
                                .transform(RoundedCorners( 10)))
                        .load(good.pict_url)
                        .into(holder.databinding.ivGoodImage)
                holder.databinding.tvGoodName.text = good.title
                holder.databinding.tvGoodVolume.text = String.format("销量 %s", good.volume)
                holder.databinding.tvGoodPrice.text = String.format("¥ %s", df.format(good.final_price.toDouble()))
                holder.databinding.tvGoodVipProfit.text = String.format("预估奖励¥%s", df.format(good.vip_commission.toDouble()))
                holder.databinding.tvGoodSvipProfit.text = String.format("会员奖励¥%s", df.format(good.svip_commission.toDouble()))
                holder.databinding.tvCouponAmount.text = String.format("¥%s", df.format(good.coupon_price.toDouble()))
                if (good.has_coupon) {
                    holder.databinding.tvCouponAmount.text = String.format("¥%s", df.format(good.coupon_price.toDouble()))
                    holder.databinding.tvCouponAmount.visibility = View.VISIBLE
                } else {
                    holder.databinding.tvCouponAmount.visibility = View.GONE
                }
            }
            else -> {
                val df = DecimalFormat("#0.00")
                val good = list[position] as TaobaoGoodBean
                holder as ViewHolderNoCoupon
                Glide.with(holder.itemView).asBitmap()
                        .apply(RequestOptions()
                                .placeholder(R.drawable.ic_good_place_holder)
                                .transform(RoundedCorners( 10)))
                        .load(good.pict_url)
                        .into(holder.databinding.ivGoodImage)
                holder.databinding.tvGoodName.text = good.title
                holder.databinding.tvGoodVolume.text = String.format("销量 %s", good.volume)
                holder.databinding.tvGoodPrice.text = String.format("¥ %s", df.format(good.final_price.toDouble()))
                holder.databinding.tvGoodVipProfit.text = String.format("预估奖励¥%s", df.format(good.vip_commission.toDouble()))
                holder.databinding.tvGoodSvipProfit.text = String.format("会员奖励¥%s", df.format(good.svip_commission.toDouble()))
                holder.databinding.tvGoodOriginalPrice.text = String.format("¥ %s", df.format(good.original_price.toDouble()))
                holder.databinding.tvGoodOriginalPrice.paintFlags = holder.databinding.tvGoodOriginalPrice.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
            }
        }
    }

    inner class ViewHolder(val databinding: GoodListItemBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    listener.click(0, list[adapterPosition])
                }
            }
        }
    }

    inner class ViewHolderNoCoupon(val databinding: GoodListItemNoCouponBinding) : androidx.recyclerview.widget.RecyclerView.ViewHolder(databinding.root) {
        init {
            databinding.root.setOnClickListener {
                if (adapterPosition != -1) {
                    listener.click(0, list[adapterPosition])
                }
            }
        }
    }
}